package com.liunian.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.liunian.subject.common.enums.IsDeletedFlagEnum;
import com.liunian.subject.domain.entity.SubjectCategoryBO;
import com.liunian.subject.domain.service.SubjectCategoryDomainService;
import com.liunian.subject.infra.basic.entity.SubjectCategory;
import com.liunian.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = subjectCategoryBO.subjectCategoryBOtoEntity();
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectCategory.setCreatedAt(LocalDateTime.now());
        return subjectCategoryService.saveSubjectCategory(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = subjectCategoryBO.subjectCategoryBOtoEntity();
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> boList = subjectCategoryList.stream().map(subjectCategoryBO::subjectCategoryEntityToBO).collect(Collectors.toList());
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryPrimaryCategory.boList:{}", JSON.toJSONString(boList));
        }
        return boList;
    }

    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = subjectCategoryBO.subjectCategoryBOtoEntity();
        int count = subjectCategoryService.updateCategory(subjectCategory);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = subjectCategoryBO.subjectCategoryBOtoEntity();
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectCategoryService.updateCategory(subjectCategory);
        return count > 0;
    }
}
